###*
 *
 *  扫描static目录下，文件名为main的js文件，作为webpack入口文件，打包文件在同级目录下main-app.js
 * @type {[type]}
###

webpack = require 'webpack'
path = require 'path'
glob = require 'glob'


###
 *
 * 区别开发和生产环境
 * @type {String}
###
_static= "static"
###
 *
 * 压缩文件后缀
 * @type {String}
###
_min= ""

###
 *
 * 生产跳转转换
###
if (process.env.NODE_ENV is 'production')
  _static= "static-dist"
  _min= ".min"


###*
 *
 * 解释static文件名为main.js的入口文件
 * @return {[type]}        [入口文件对象]
###
getEntry = ->
  pattern= path.resolve __dirname, "static/**/main.js"
  arrPaths= glob.sync pattern
  arrEntry= {}

  arrPaths.map (_path) ->
    _dir= new RegExp (path.resolve __dirname, "static/(.*).js"), 'g'
    _dir.test _path

    arrEntry[RegExp.$1]= _path

  arrEntry


###
 *
 * webpack主配置
 * @type {[type]}
###
module.exports =
    entry: getEntry(),
    output:
      path: path.resolve __dirname, _static
      filename: "[name]-app#{_min}.js"

    module:
      loaders: [
          { test: /\.vue$/, loader: 'vue'}

          { test: /\.js$/, loader: 'babel', exclude: /node_modules|bower_components/}

          { test: /\.html$/, loader: 'html-loader'}

          { test: /\.css$/, loader: 'style-loader!css-loader'}

          {test: /\.(woff|woff2|eot|ttf)$/i,   loader: "file-loader?name=fonts/[name]-[hash].[ext]"}

          {test: /\.(jpg|png|jpeg|svg)$/i,   loader: "file-loader"}

          {test: /\.styl$/, loader: 'style-loader!css-loader!stylus-loader' }

          # {test: /\.(json)$/i,   loader: "json-loader"}

          {test: /\.ejs$/, loader: 'ejs-loader'}

          # {test: /\.less$/, loader: "style!css!less"}
      ]

    resolve:
      modulesDirectories: ["node_modules", "bower_components"]

    externals:
      'jquery': 'window.$'
      'layer': 'window.layer'

    plugins: [
      new webpack.ResolverPlugin(
          [new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin(".bower.json", ["main"])]
      )
    ]

    babel:
      presets: ['es2015'],
      plugins: ['transform-runtime']

###
 *
 * 生产模式
###
if (process.env.NODE_ENV is 'production')
  # module.exports.devtool = '#source-map'
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),
    new webpack.optimize.OccurenceOrderPlugin()
  ])
